package buber.config;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;

public class Config {
	private static Config cfg;
	private double mutationRatio=0.1d;
	private int chromosomes=1;
	private int popSize=20;
	private int generations=100;
	private int chSize=20;
	
	public int getChSize() {
		return chSize;
	}
	public void setChSize(int chSize) {
		this.chSize = chSize;
	}
	public double getMutationRatio() {
		return mutationRatio;
	}
	public void setMutationRatio(double mutationRatio) {
		this.mutationRatio = mutationRatio;
	}
	public int getChromosomes() {
		return chromosomes;
	}
	public void setChromosomes(int chromosomes) {
		this.chromosomes = chromosomes;
	}
	public int getPopSize() {
		return popSize;
	}
	public void setPopSize(int popSize) {
		this.popSize = popSize;
	}
	public int getGenerations() {
		return generations;
	}
	public void setGenerations(int generations) {
		this.generations = generations;
	}
	private Config(){
		loadProperties();
	}
	public static Config getInstance(){
		if(cfg==null){
			cfg=new Config();
			return cfg;
		}
		else{
			return cfg;
		}
	}
	private void loadProperties(){
		Properties prop = new Properties();
		try {
			prop.loadFromXML(new FileInputStream(new File("cfg/buber-cfg.xml")));
			setMutationRatio(Double.parseDouble(prop.getProperty("mutationRatio")));
			setChromosomes(Integer.parseInt(prop.getProperty("chromosomes")));
			setGenerations(Integer.parseInt(prop.getProperty("generations")));
			setPopSize(Integer.parseInt(prop.getProperty("popSize")));
			setChSize(Integer.parseInt(prop.getProperty("chromosomeSize")));
		} catch (InvalidPropertiesFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (NumberFormatException e){
			System.out.println("[config] Error reading config. Using default.");
		}
	}
}
